package com.xingzhi.kotlindemo;

/**
 * Date:2025/3/24
 * Time:23:31
 * author:wushengqi
 */
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class CustomHorizontalScrollView extends HorizontalScrollView {

    private GestureDetector gestureDetector;
    private int childWidth; // 子视图的宽度
    private int currentScrollX; // 当前滚动位置

    public CustomHorizontalScrollView(Context context) {
        this(context, null);
    }

    public CustomHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                // 判断滑动方向
                if (e1.getX() - e2.getX() > 0) {
                    // 向左滑动
                    scrollToNextChild(true);
                } else if (e2.getX() - e1.getX() > 0) {
                    // 向右滑动
                    scrollToNextChild(false);
                }
                return true;
            }
        });
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (getChildCount() > 0) {
            View child = getChildAt(0);
            if (child instanceof LinearLayout) {
                LinearLayout linearLayout = (LinearLayout) child;
                for (int i = 0; i < linearLayout.getChildCount(); i++) {
                    View childView = linearLayout.getChildAt(i);
                    childWidth = childView.getWidth();
                    break; // 假设所有子视图宽度相同，取第一个子视图的宽度
                }
            }
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        currentScrollX = l;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return gestureDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
    }

    private void scrollToNextChild(boolean isLeft) {
        int childCount = ((LinearLayout) getChildAt(0)).getChildCount();
        int targetScrollX = 0;

        if (isLeft) {
            // 向左滑动，找到当前视图左边的子视图
            int currentIndex = currentScrollX / childWidth;
            if (currentIndex > 0) {
                targetScrollX = currentIndex * childWidth;
            }
        } else {
            // 向右滑动，找到当前视图右边的子视图
            int currentIndex = (currentScrollX + getWidth()) / childWidth;
            if (currentIndex < childCount - 1) {
                targetScrollX = (currentIndex + 1) * childWidth - getWidth();
            } else if (currentIndex == childCount - 1) {
                // 如果已经是最右边的子视图，则滚动到最右边
                targetScrollX = childCount * childWidth - getWidth();
            }
        }

        smoothScrollTo(targetScrollX, 0);
    }
}

